package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageInfo;
import haizhi.fruitmall.enumeration.GoodsOrderByEnum;
import haizhi.fruitmall.pojo.form.GoodsCategoryForm;
import haizhi.fruitmall.pojo.form.GoodsForm;
import haizhi.fruitmall.pojo.form.GoodsUnitForm;
import haizhi.fruitmall.pojo.po.Goods;
import haizhi.fruitmall.pojo.po.GoodsUnit;
import haizhi.fruitmall.pojo.view.GoodsCategoryView;
import haizhi.fruitmall.pojo.view.GoodsOrderByView;
import haizhi.fruitmall.pojo.view.GoodsUnitView;
import haizhi.fruitmall.service.GoodsCategoryService;
import haizhi.fruitmall.service.GoodsService;
import haizhi.fruitmall.service.GoodsUnitService;
import haizhi.fruitmall.service.ListService;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2017/12/24 20:52
 * @Author: Richard Zane
 */

@Service
@Transactional
public class ListServiceImpl implements ListService {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsUnitService goodsUnitService;

    //接收categoryId
    @Override
    public Map getHead(GoodsCategoryForm goodsCategoryForm){
        Map<String,Object> dataMap = new HashMap<>();
        //商品分类
        List<GoodsCategoryView> goodsCategoryViewList = goodsCategoryService.getView(goodsCategoryForm);
        dataMap.put("goodsCategory",goodsCategoryViewList);
        //商品排序
        List<GoodsOrderByView> goodsOrderByViewList = new ArrayList<>();
        goodsOrderByViewList.add(new GoodsOrderByView(GoodsOrderByEnum.DEFAULT));
        goodsOrderByViewList.add(new GoodsOrderByView(GoodsOrderByEnum.PRICE_LOW));
        goodsOrderByViewList.add(new GoodsOrderByView(GoodsOrderByEnum.PRICE_HIGH));
        goodsOrderByViewList.add(new GoodsOrderByView(GoodsOrderByEnum.COMMENT_TIMES));
        goodsOrderByViewList.add(new GoodsOrderByView(GoodsOrderByEnum.CREATE_TIME));
        dataMap.put("orderBy", goodsOrderByViewList);
        return dataMap;
    }
    //接收categoryId,orderBy,pageNum,pageSize
    @Override
    public Map getTail(GoodsForm goodsForm){
        Map<String,Object> dataMap = new HashMap<>();
        //分类商品
        List<Goods> goodsList = goodsService.getall(goodsForm);
        PageInfo pageInfo = new PageInfo(goodsList);
        if(BeanUtil.noEmpty(goodsList)) {
            List<Long> unitIdList = new ArrayList<>();
            for (Goods goods : goodsList) {
                unitIdList.add(goods.getUnitId());
            }
            //接收unitIdList和orderBy
            List<GoodsUnitView> goodsUnitViewList = new ArrayList<>();
            GoodsUnitForm goodsUnitForm = new GoodsUnitForm();
            goodsUnitForm.setOrderBy(goodsForm.getOrderBy());
            goodsUnitForm.setUnitIds(unitIdList);
            List<GoodsUnit> goodsUnitList = goodsUnitService.getall(goodsUnitForm);
            for(GoodsUnit goodsUnit : goodsUnitList) {
                GoodsUnitView goodsUnitView = new GoodsUnitView();
                BeanUtil.copyBean(goodsUnit, goodsUnitView);
                goodsUnitViewList.add(goodsUnitView);
            }
            pageInfo.setList(goodsUnitViewList);
            dataMap.put("goodsUnit",pageInfo);
        }
        return dataMap;
    }

}
